﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Models;
using Estsh.Core.Services;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Estsh.Core.Wms.Services
{
    /// <summary>
    /// NC入库
    /// </summary>
    public class NCInStockService : BaseService<BaseEntity>, INCInStockService
    {
        private readonly INCInStockRepository repository;
        public NCInStockService(INCInStockRepository _repository) : base(_repository)
        {
            repository = _repository;
        }
        //获取所有NC入库单据信息
        public SetObjectDetail GetNCInStockOrderList(string orderNo)
        {

            SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
            List<WmsMoveHeader> wmsMoves = repository.GetNCInStockOrderList(orderNo);

            if (wmsMoves.Count > 0)
            {
                rfInfo.type = "PASS";
                rfInfo.message = "成功";
                rfInfo.wmsObjectDetail = wmsMoves.ConvertAll(s => (object)s);
            }
            else
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "未查询到数据，请检查！";
            }
            return rfInfo;
        }
        public SetObjectDetail GetNCInStockOrderListByOrderNo(string orderNo)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();

            List<WmsMoveHeader> wmsMoves = repository.GetNCInStockOrderListByOrderNo(orderNo);
            if (wmsMoves.Count > 0)
            {
                rfInfo.type = "PASS";
                rfInfo.message = "成功";
                rfInfo.wmsObjectDetail = wmsMoves.ConvertAll(s => (object)s);
            }
            else
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "未查询到数据，请检查！";
            }
            return rfInfo;
        }

        public SetObjectDetail GetLocateByName(string locateName)
        {

            SetObjectDetail rfInfo = new SetObjectDetail();
            //查询库位在库信息
            List<SysLocate> sysLocates = repository.GetLocateByLocateName(locateName);

            //库存总数
            SysStock sysStocksLocateCount = repository.GetStocQtySumByLocateName(locateName);

            if (sysLocates.Count == 0)
            {
                //提示  [A1-2-2]:该库位不存在，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + locateName + "]:该库位不存在，请检查!";
                return rfInfo;
            }
            else if (sysLocates[0].Enabled != "Y")
            {
                //提示  [A1-2-2]:该库位不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + locateName + "]:该库位不可用，请检查!";
                return rfInfo;
            }
            else if (sysLocates[0].LocateType != (int)WmsEnumUtil.LocateType.NC_LOCATE)
            {
                //提示  [A1-2-2]:此库位码类型不是NC库位类型，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + locateName + "]:此库位码类型不是NC库位类型，请检查!";
                return rfInfo;
            }
            else if ((decimal)sysStocksLocateCount.Qty >= sysLocates[0].LocateCapacity)
            {
                //提示  [A1-2-2]:该库位容量已满，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + locateName + "]:该库位容量已满，请检查!";
                return rfInfo;
            }

            rfInfo.type = "PASS";
            rfInfo.message = "[" + locateName + "]:扫描成功,请扫描箱条码";
            return rfInfo;
        }

        public SetObjectDetail CheckStockByCartonAlocate(string cartonNo, string locateName, string orderNo, string loginId)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();
            List<SysStock> sysStocks = repository.GetStocInfoByCartonNo(cartonNo);

            if (sysStocks.Count == 0)
            {
                //提示  [M0090105]:该条码不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码不存在，请检查!";
                return rfInfo;
            }
            else if (sysStocks[0].Enabled != "Y")
            {
                //提示  [M0090105]:箱条码已冻结，无法操作!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:箱条码已冻结，无法操作!";
                return rfInfo;
            }
			//else if (sysStocks[0].Status != (int)WmsEnumUtil.StockStatus.INSTOCKED && sysStocks[0].Status != (int)WmsEnumUtil.StockStatus.NC_QUARANTINE)
			//已上架、已配料、已上线 NC隔离状态
			else if (sysStocks[0].Status != (int)WmsEnumUtil.StockStatus.INSTOCKED && sysStocks[0].Status != (int)WmsEnumUtil.StockStatus.BATCHED && sysStocks[0].Status != (int)WmsEnumUtil.StockStatus.ONLINED && sysStocks[0].Status != (int)WmsEnumUtil.StockStatus.NC_QUARANTINE)
            {
                //提示  [M0090105]:该条码是NC隔离状态，无法操作!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码是[" + sysStocks[0].StockStatus + "]状态，请检查!";
                return rfInfo;
            }

            //根据库位获取对应：绑定零件号
            List<SysLocate> sysLocates = repository.GetLocatePartByLocateName(locateName);

            //根据库位名称获取目标信息：ERP库位
            List<SysLocate> locatesZone = repository.GetLocateZoneByLocateName(locateName);

            //根据单据获取主表信息
            List<WmsMoveHeader> wmsMoveHeaders = repository.GetMoveHeaderByOrderNo(orderNo);
            if (wmsMoveHeaders.Count == 0)
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + orderNo + "]:该单据是未创建或已关闭状态，请检查!";
                return rfInfo;
            }

            List<WmsMoveDetail> wmsMoveDetails = repository.GetMoveDetailByOrderNoPartNo(orderNo, sysStocks[0].PartNo);
            if (wmsMoveDetails.Count == 0)
            {
                //提示  [M0090105]:该条码是NC入库状态，无法操作!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码零件号不在单据内!";
                return rfInfo;
            }
            List<WmsMoveSn> moveSnCreates = repository.GetMoveSnByOrderNo(orderNo);
            if (moveSnCreates.Count > 0)
            {
                List<WmsMoveSn> wmsMoveSns = repository.GetMoveSnByOrderNoCarton(orderNo, cartonNo);
                if (wmsMoveSns.Count == 0)
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "单据中不包含此包装条码，不允许此操作！";
                    return rfInfo;
                }
            }
            StringBuilder stringBuilder = new StringBuilder(1024);

            List<string> sqlStrings = new List<string>();
            List<DynamicParameters> parameterList = new List<DynamicParameters>();
            for (int i = 0; i < wmsMoveDetails.Count; i++)
            {
                if (wmsMoveDetails[i].Qty >= wmsMoveDetails[i].PickQty)
                {
                    int remainNum = Convert.ToInt32(wmsMoveDetails[i].Qty - wmsMoveDetails[i].PickQty);//余数
                    if (sysStocks[0].Qty == remainNum)
                    {
                        if (wmsMoveHeaders[0].OrderStatus == 10)//修改为单据处理中状态
                        {
                            stringBuilder = new StringBuilder(1024);
                            stringBuilder.Append("UPDATE wms_move_header SET order_status='30',update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no='" + orderNo + "';");
                            sqlStrings.Add(stringBuilder.ToString());
                        }
                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE wms_move_detail SET pick_qty+=" + sysStocks[0].Qty + ",item_status=" + (int)WmsEnumUtil.MoveOrderDetailStatus.COMPLETED + " " +
                            ",dest_warehouse_id=" + locatesZone[0].WarehouseId + ",dest_warehouse_name='" + locatesZone[0].WarehouseName + "',dest_zone_id=" + locatesZone[0].ZoneId + ",dest_zone_name='" + locatesZone[0].ZoneName + "',dest_erp_warehouse='" + locatesZone[0].ErpWarehouse + "'" +
                            ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  ruid='" + wmsMoveDetails[i].Ruid + "';");
                        sqlStrings.Add(stringBuilder.ToString());

                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE sys_stock SET status=90,locate_id=" + sysLocates[0].LocateId + ",locate_name='" + locateName + "',zone_id=" + sysLocates[0].ZoneId + ",zone_name='" + sysLocates[0].ZoneName + "',warehouse_id=" + sysLocates[0].WarehouseId + ",warehouse_name='" + sysLocates[0].WarehouseName + "',update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + cartonNo + "';");
                        sqlStrings.Add(stringBuilder.ToString());

                        if (moveSnCreates.Count == 0)
                        {
                            stringBuilder = new StringBuilder(1024);
                            stringBuilder.Append("INSERT INTO dbo.wms_move_sn ");
                            stringBuilder.Append("( ");
                            stringBuilder.Append("    order_no, ");
                            stringBuilder.Append("    item_no, ");
                            stringBuilder.Append("    part_id, ");
                            stringBuilder.Append("    part_no, ");
                            stringBuilder.Append("    part_spec, ");
                            stringBuilder.Append("    carton_no, ");
                            stringBuilder.Append("    qty, ");
                            stringBuilder.Append("    unit, ");
                            stringBuilder.Append("    status, ");
                            stringBuilder.Append("    factory_id, ");
                            stringBuilder.Append("    factory_code, ");
                            stringBuilder.Append("    enabled, ");
                            stringBuilder.Append("    create_userid, ");
                            stringBuilder.Append("    create_time, ");
                            stringBuilder.Append("    guid ");
                            stringBuilder.Append(") ");
                            stringBuilder.Append("VALUES ");
                            stringBuilder.Append("(   N'" + orderNo + "', ");
                            stringBuilder.Append("    '" + wmsMoveDetails[i].ItemNo + "',");
                            stringBuilder.Append("    " + wmsMoveDetails[i].PartId + ", ");
                            stringBuilder.Append("    N'" + wmsMoveDetails[i].PartNo + "',");
                            stringBuilder.Append("    N'" + wmsMoveDetails[i].PartSpec + "',");
                            stringBuilder.Append("    '" + cartonNo + "',");
                            stringBuilder.Append("    " + sysStocks[0].Qty + ", ");
                            stringBuilder.Append("    '" + sysStocks[0].Unit + "',");
                            stringBuilder.Append("    " + (int)WmsEnumUtil.MoveOrderSnStatus.PROCESSED + ",");//处理中
                            stringBuilder.Append("    " + sysStocks[0].FactoryId + ", ");
                            stringBuilder.Append("    N'" + sysStocks[0].FactoryCode + "', ");
                            stringBuilder.Append("    'Y', ");
                            stringBuilder.Append("   " + loginId + " ,  ");
                            stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
                            stringBuilder.Append("    newid()");
                            stringBuilder.Append("    );");
                            sqlStrings.Add(stringBuilder.ToString());

                        }
                        else
                        {
                            stringBuilder = new StringBuilder(1024);
                            //更新单据条码表
                            stringBuilder.Append("UPDATE dbo.wms_move_sn ");
                            stringBuilder.Append("SET status = " + (int)WmsEnumUtil.MoveOrderSnStatus.PROCESSED + ", ");
                            stringBuilder.Append("    update_userid = " + loginId + ", ");
                            stringBuilder.Append("    update_time = CONVERT(VARCHAR(20),GETDATE(),21) ");
                            stringBuilder.Append("WHERE order_no = '" + orderNo + "' ");
                            stringBuilder.Append("      AND carton_no = '" + cartonNo + "' ");
                            sqlStrings.Add(stringBuilder.ToString());

                        }
                    }
                    else if (sysStocks[0].Qty > remainNum)
                    {
                        //stringBuilder.Append("UPDATE wms_move_detail SET pick_qty+=" + remainNum + ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  ruid='" + wmsMoveDetails[i].Ruid + "';");
                        //提示  [M0090105]:该条码是NC入库状态，无法操作!
                        rfInfo.type = "FAIL";
                        rfInfo.message = "[" + cartonNo + "]:该条码库存大于单据需求数量!";
                        return rfInfo;
                    }
                    else if (sysStocks[0].Qty < remainNum)
                    {
                        if (wmsMoveHeaders[0].OrderStatus == 10)//修改为单据处理中状态
                        {
                            stringBuilder = new StringBuilder(1024);
                            stringBuilder.Append("UPDATE wms_move_header SET order_status='30',update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no='" + orderNo + "';");
                            sqlStrings.Add(stringBuilder.ToString());
                        }
                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE wms_move_detail SET pick_qty+=" + sysStocks[0].Qty + ",item_status=" + (int)WmsEnumUtil.MoveOrderDetailStatus.BATCHING + "" +
                            ",dest_warehouse_id=" + locatesZone[0].WarehouseId + ",dest_warehouse_name='" + locatesZone[0].WarehouseName + "',dest_zone_id=" + locatesZone[0].ZoneId + ",dest_zone_name='" + locatesZone[0].ZoneName + "',dest_erp_warehouse='" + locatesZone[0].ErpWarehouse + "'" +
                            ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  ruid='" + wmsMoveDetails[i].Ruid + "';");
                        sqlStrings.Add(stringBuilder.ToString());

                        stringBuilder = new StringBuilder(1024);
                        stringBuilder.Append("UPDATE sys_stock SET status=90,locate_id=" + sysLocates[0].LocateId + ",locate_name='" + locateName + "',zone_id=" + sysLocates[0].ZoneId + ",zone_name='" + sysLocates[0].ZoneName + "',warehouse_id=" + sysLocates[0].WarehouseId + ",warehouse_name='" + sysLocates[0].WarehouseName + "',update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + cartonNo + "';");
                        sqlStrings.Add(stringBuilder.ToString());

                        if (moveSnCreates.Count == 0)
                        {
                            stringBuilder = new StringBuilder(1024);
                            stringBuilder.Append("INSERT INTO dbo.wms_move_sn ");
                            stringBuilder.Append("( ");
                            stringBuilder.Append("    order_no, ");
                            stringBuilder.Append("    item_no, ");
                            stringBuilder.Append("    part_id, ");
                            stringBuilder.Append("    part_no, ");
                            stringBuilder.Append("    part_spec, ");
                            stringBuilder.Append("    carton_no, ");
                            stringBuilder.Append("    qty, ");
                            stringBuilder.Append("    unit, ");
                            stringBuilder.Append("    status, ");
                            stringBuilder.Append("    factory_id, ");
                            stringBuilder.Append("    factory_code, ");
                            stringBuilder.Append("    enabled, ");
                            stringBuilder.Append("    create_userid, ");
                            stringBuilder.Append("    create_time, ");
                            stringBuilder.Append("    guid ");
                            stringBuilder.Append(") ");
                            stringBuilder.Append("VALUES ");
                            stringBuilder.Append("(   N'" + orderNo + "', ");
                            stringBuilder.Append("    '" + wmsMoveDetails[i].ItemNo + "',");
                            stringBuilder.Append("    " + wmsMoveDetails[i].PartId + ", ");
                            stringBuilder.Append("    N'" + wmsMoveDetails[i].PartNo + "',");
                            stringBuilder.Append("    N'" + wmsMoveDetails[i].PartSpec + "',");
                            stringBuilder.Append("    '" + cartonNo + "',");
                            stringBuilder.Append("    " + sysStocks[0].Qty + ", ");
                            stringBuilder.Append("    '" + sysStocks[0].Unit + "',");
                            stringBuilder.Append("    " + (int)WmsEnumUtil.MoveOrderSnStatus.PROCESSED + ",");//处理中
                            stringBuilder.Append("    " + sysStocks[0].FactoryId + ", ");
                            stringBuilder.Append("    N'" + sysStocks[0].FactoryCode + "', ");
                            stringBuilder.Append("    'Y', ");
                            stringBuilder.Append("   " + loginId + " ,  ");
                            stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
                            stringBuilder.Append("    newid()");
                            stringBuilder.Append("    );");
                            sqlStrings.Add(stringBuilder.ToString());
                        }
                        else
                        {
                            stringBuilder = new StringBuilder(1024);
                            //更新单据条码表
                            stringBuilder.Append("UPDATE dbo.wms_move_sn ");
                            stringBuilder.Append("SET status = " + (int)WmsEnumUtil.MoveOrderSnStatus.PROCESSED + ", ");
                            stringBuilder.Append("    update_userid = " + loginId + ", ");
                            stringBuilder.Append("    update_time = CONVERT(VARCHAR(20),GETDATE(),21) ");
                            stringBuilder.Append("WHERE order_no = '" + orderNo + "' ");
                            stringBuilder.Append("      AND carton_no = '" + cartonNo + "' ");
                            sqlStrings.Add(stringBuilder.ToString());
                        }
                    }
                }
                else
                {
                    //提示  [M0090105]:该条码是NC入库状态，无法操作!
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:该条码库存大于单据需求数量!";
                    return rfInfo;
                }

                stringBuilder = new StringBuilder(1024);
                stringBuilder.Append("INSERT INTO dbo.sys_stock_trans ");
                stringBuilder.Append("( ");
                stringBuilder.Append("    trans_code, ");
                stringBuilder.Append("    carton_no, ");
                stringBuilder.Append("    part_id, ");
                stringBuilder.Append("    part_no, ");
                stringBuilder.Append("    part_spec, ");
                stringBuilder.Append("    src_locate_id, ");
                stringBuilder.Append("    src_locate_name, ");
                stringBuilder.Append("    dest_locate_id, ");
                stringBuilder.Append("    dest_locate_name, ");
                stringBuilder.Append("    old_qty, ");
                stringBuilder.Append("    new_qty, ");
                stringBuilder.Append("    trans_qty, ");
                stringBuilder.Append("    old_status, ");
                stringBuilder.Append("    new_status, ");
                stringBuilder.Append("    unit, ");
                stringBuilder.Append("    factory_id, ");
                stringBuilder.Append("    factory_code, ");
                stringBuilder.Append("    src_erp_warehouse, ");
                stringBuilder.Append("    dest_erp_warehouse, ");
                stringBuilder.Append("    src_warehouse_id, ");
                stringBuilder.Append("    src_warehouse_name, ");
                stringBuilder.Append("    dest_warehouse_id, ");
                stringBuilder.Append("    dest_warehouse_name, ");
                stringBuilder.Append("    src_zone_id, ");
                stringBuilder.Append("    src_zone_name, ");
                stringBuilder.Append("    dest_zone_id, ");
                stringBuilder.Append("    dest_zone_name, ");
                stringBuilder.Append("    enabled, ");
                stringBuilder.Append("    create_userid, ");
                stringBuilder.Append("    create_time, ");
                stringBuilder.Append("    update_userid, ");
                stringBuilder.Append("    update_time, ");
                stringBuilder.Append("    guid ");
                stringBuilder.Append(") ");
                stringBuilder.Append("VALUES ");
                stringBuilder.Append("(   '" + (int)WmsEnumUtil.TransType.NC_INSTOCK + "', ");//NC状态
                stringBuilder.Append("    '" + sysStocks[0].CartonNo + "', ");
                stringBuilder.Append("    " + sysStocks[0].PartId + ", ");
                stringBuilder.Append("    '" + sysStocks[0].PartNo + "',  ");
                stringBuilder.Append("    N'" + sysStocks[0].PartSpec + "', ");
                stringBuilder.Append("    " + sysStocks[0].LocateId == null ? 0 : sysStocks[0].LocateId + ",  ");//源库位
                stringBuilder.Append("    '" + sysStocks[0].LocateName + "',  ");
                stringBuilder.Append("    " + sysLocates[0].LocateId + ", ");//目标库位
                stringBuilder.Append("    '" + sysLocates[0].LocateName + "',  ");
                stringBuilder.Append("    " + sysStocks[0].Qty + ",");
                stringBuilder.Append("    " + sysStocks[0].Qty + ",");
                stringBuilder.Append("    " + sysStocks[0].Qty + ",");
                stringBuilder.Append("    '" + sysStocks[0].Status + "', ");
                stringBuilder.Append("    '90', ");
                stringBuilder.Append("    '" + sysStocks[0].Unit + "', ");
                stringBuilder.Append("    " + sysStocks[0].FactoryId + ", ");
                stringBuilder.Append("    '" + sysStocks[0].FactoryCode + "',");
                stringBuilder.Append("    '" + sysStocks[0].ErpWarehouse + "',");
                stringBuilder.Append("    '" + sysStocks[0].ErpWarehouse + "', ");
                stringBuilder.Append("    " + sysStocks[0].WarehouseId == null ? 0 : sysStocks[0].WarehouseId + ", ");
                stringBuilder.Append("    '" + sysStocks[0].WarehouseName + "', ");
                stringBuilder.Append("    " + sysLocates[0].WarehouseId + ", ");
                stringBuilder.Append("    '" + sysLocates[0].WarehouseName + "', ");
                stringBuilder.Append("    " + sysStocks[0].ZoneId == null ? 0 : sysStocks[0].ZoneId + ",  ");
                stringBuilder.Append("    '" + sysStocks[0].ZoneName + "',  ");
                stringBuilder.Append("    " + sysLocates[0].ZoneId + ", ");
                stringBuilder.Append("    '" + sysLocates[0].ZoneName + "',  ");
                stringBuilder.Append("    'Y', ");
                stringBuilder.Append("    " + loginId + ", ");
                stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
                stringBuilder.Append("    " + loginId + ", ");
                stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
                stringBuilder.Append("    NEWID()  ");
                stringBuilder.Append("    ) ; ");
                sqlStrings.Add(stringBuilder.ToString());
            }

            int refNum = repository.SQLTransaction(sqlStrings, parameterList);
            if (refNum > 0)
            {
                List<WmsMoveHeader> wmsMoves = repository.GetMoveHeaderInfoByOrderNo(orderNo);

                if (wmsMoves.Count > 0)
                {
                    List<WmsMoveHeader> wmsMoves1 = repository.GetMoveHeaderQtySumByOrderNo(orderNo);
                    for (int i = 0; i < wmsMoves1.Count; i++)
                    {
                        if (wmsMoves1[i].Qty != wmsMoves1[i].PickQty)
                        {
                            rfInfo.type = "PASS";
                            rfInfo.message = "操作成功!请继续扫描箱条码";
                            rfInfo.wmsObjectDetail = wmsMoves1.ConvertAll(s => (object)s);
                            return rfInfo;
                        }
                    }
                    int updateStatus = 1;
                    sqlStrings = new List<string>();
                    parameterList = new List<DynamicParameters>();
                    StringBuilder SqlStringBuilder = new StringBuilder(1024);
                    DynamicParameters parameters = new DynamicParameters();

                    List<WmsMoveDetail_Extend> moveDetails = repository.GetFinishMoveDetailByOrderNoAutoPostBack(orderNo);
                    if (moveDetails.Count > 0)
                    {
                        SqlStringBuilder = new StringBuilder(1024);
                        SqlStringBuilder.Append("INSERT INTO dbo.WMS_MOVR(ZINSTNO,ZEILE,WERKS,LGORT,UMLGO,MATNR,ERFMG ");
                        SqlStringBuilder.Append("           ,ERFME,ACTION,ZPOST,ZTIME,REFORDERNO,SID,RECTIM,SYNFLG ");
                        SqlStringBuilder.Append("           ,GUID) ");
                        SqlStringBuilder.Append("     VALUES(@ZINSTNO,@ZEILE,@WERKS,@LGORT,@UMLGO,@MATNR,@ERFMG,@ERFME ");
                        SqlStringBuilder.Append("           ,@ACTION,@ZPOST,@ZTIME,@REFORDERNO,@SID,@RECTIM,@SYNFLG ");
                        SqlStringBuilder.Append("           ,@GUID) ");

                        for (int i = 0; i < moveDetails.Count; i++)
                        {
                            SysPart sysPart = repository.GetPartNOInfoByPartNo(moveDetails[i].PartNo);
                            sqlStrings.Add(SqlStringBuilder.ToString());
                            parameters = new DynamicParameters();
                            parameters.Add("@ZINSTNO", moveDetails[i].OrderNo);//盘点凭证号
                            parameters.Add("@ZEILE", (i + 1) * 10);//行号
                            parameters.Add("@WERKS", moveDetails[i].FactoryCode);//工厂
                            parameters.Add("@LGORT", moveDetails[i].SrcErpWarehouse);//发出库存地点
                            parameters.Add("@UMLGO", moveDetails[i].DestErpWarehouse);//接收库存地点

                            parameters.Add("@MATNR", moveDetails[i].PartNo);//物料编码
                            parameters.Add("@ERFMG", moveDetails[i].PickQty);//转储数量
                            if (sysPart == null)
                            {
                                parameters.Add("@ERFME", "");//计量单位
                            }
                            else
                            {
                                parameters.Add("@ERFME", sysPart.Unit);//计量单位
                            }
                            parameters.Add("@ACTION", "M01"); //M01: NC入库
                            parameters.Add("@ZPOST", DateTime.Now.ToString("yyyyMMdd"));//交易日期
                            parameters.Add("@ZTIME", DateTime.Now.ToString("HHmmss"));//交易时间
							parameters.Add("@REFORDERNO", moveDetails[i].refOrderNo);//关联单号
							parameters.Add("@SID", Guid.NewGuid().ToString().Replace("-", ""));
                            parameters.Add("@RECTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                            parameters.Add("@SYNFLG", "N");
                            parameters.Add("@GUID", Guid.NewGuid().ToString());
                            parameterList.Add(parameters);
                        }
                        SqlStringBuilder = new StringBuilder(1024);
                        SqlStringBuilder.Append("UPDATE wms_move_header SET order_status=" + (int)WmsEnumUtil.MoveOrderStatus.COMPLETED + ", update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no=@orderNo;");
                        sqlStrings.Add(SqlStringBuilder.ToString());
                        parameters = new DynamicParameters();
                        parameters.Add("@orderNo", orderNo);
                        parameterList.Add(parameters);

                        SqlStringBuilder = new StringBuilder(1024);
                        SqlStringBuilder.Append("UPDATE wms_move_detail SET item_status=" + (int)WmsEnumUtil.MoveOrderDetailStatus.COMPLETED + ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no=@orderNo;");
                        sqlStrings.Add(SqlStringBuilder.ToString());
                        parameters = new DynamicParameters();
                        parameters.Add("@orderNo", orderNo);
                        parameterList.Add(parameters);

                        updateStatus = repository.SQLTransaction(sqlStrings, parameterList);
                    }
                    if (updateStatus > 0)
                    {
                        rfInfo.type = "PASS";
                        rfInfo.message = "单据操作完成!请切换单据";
                    }
                    else
                    {
                        rfInfo.type = "FAIL";
                        rfInfo.message = "单据操作操作失败!请检查";
                    }
                }
                else
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "未查询到数据，请检查！";
                }
            }
            else
            {
                //提示  操作失败!
                rfInfo.type = "FAIL";
                rfInfo.message = "未查询到数据，请检查！";
            }
            return rfInfo;
        }
        public SetObjectDetail SetNCInStockOrderNoSubmit(string orderNo, string loginId)
        {
            StringBuilder stringBuilder = new StringBuilder(1024);
            List<string> sqlStrings = new List<string>();
            List<DynamicParameters> parameterList = new List<DynamicParameters>();
            SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
            StringBuilder SqlStringBuilder = new StringBuilder(1024);
            List<WmsMoveDetail_Extend> wmsMoves = repository.GetFinishMoveDetailByOrderNo(orderNo);
            DynamicParameters parameters = new DynamicParameters();
            int updateStatus = 1;
            if (wmsMoves.Count == 0)
            {
                SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("INSERT INTO dbo.WMS_MOVR(ZINSTNO,ZEILE,WERKS,LGORT,UMLGO,MATNR,ERFMG ");
                SqlStringBuilder.Append("           ,ERFME,ACTION,ZPOST,ZTIME ,REFORDERNO , SID,RECTIM,SYNFLG ");
                SqlStringBuilder.Append("           ,GUID) ");
                SqlStringBuilder.Append("     VALUES(@ZINSTNO,@ZEILE,@WERKS,@LGORT,@UMLGO,@MATNR,@ERFMG,@ERFME ");
                SqlStringBuilder.Append("           ,@ACTION,@ZPOST,@ZTIME ,@REFORDERNO ,@SID,@RECTIM,@SYNFLG ");
                SqlStringBuilder.Append("           ,@GUID) ");

                for (int i = 0; i < wmsMoves.Count; i++)
                {
                    SysPart sysPart = repository.GetPartNOInfoByPartNo(wmsMoves[i].PartNo);
                    sqlStrings.Add(SqlStringBuilder.ToString());
                    parameters = new DynamicParameters();
                    parameters.Add("@ZINSTNO", wmsMoves[i].OrderNo);//编号
					parameters.Add("@ZEILE", (i + 1) * 10);//行号
                    parameters.Add("@WERKS", wmsMoves[i].FactoryCode);//工厂
                    parameters.Add("@LGORT", wmsMoves[i].SrcErpWarehouse);//发出库存地点
                    parameters.Add("@UMLGO", wmsMoves[i].DestErpWarehouse);//接收库存地点
                    parameters.Add("@MATNR", wmsMoves[i].PartNo);//物料编码
                    parameters.Add("@ERFMG", wmsMoves[i].PickQty);//转储数量
                    if (sysPart == null)
                    {
                        parameters.Add("@ERFME", "");//计量单位
                    }
                    else
                    {
                        parameters.Add("@ERFME", sysPart.Unit);//计量单位
                    }
                    parameters.Add("@ACTION", "M01"); //  业务类型: M01: NC入库
					parameters.Add("@ZPOST", DateTime.Now.ToString("yyyyMMdd"));//交易日期
                    parameters.Add("@ZTIME", DateTime.Now.ToString("HHmmss"));//交易时间
					parameters.Add("@REFORDERNO", wmsMoves[i].refOrderNo);//关联单号
					parameters.Add("@SID", Guid.NewGuid().ToString().Replace("-", ""));
                    parameters.Add("@RECTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    parameters.Add("@SYNFLG", "N");
					parameters.Add("@GUID", Guid.NewGuid().ToString());
                    parameterList.Add(parameters);
                }
                SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("UPDATE wms_move_header SET order_status=" + (int)WmsEnumUtil.MoveOrderDetailStatus.COMPLETED + ", update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no=@orderNo;");
                sqlStrings.Add(SqlStringBuilder.ToString());
                parameters = new DynamicParameters();
                parameters.Add("@orderNo", orderNo);
                parameterList.Add(parameters);

                SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("UPDATE wms_move_detail SET item_status=" + (int)WmsEnumUtil.MoveOrderDetailStatus.COMPLETED + ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no=@orderNo;");
                sqlStrings.Add(SqlStringBuilder.ToString());
                parameters = new DynamicParameters();
                parameters.Add("@orderNo", orderNo);
                parameterList.Add(parameters);
                updateStatus = repository.SQLTransaction(sqlStrings, parameterList);
            }
            else
            {
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + orderNo + "]单据未完成，无法提交";
				return rfInfo;
			}

            if (updateStatus > 0)
            {
                rfInfo.type = "PASS";
                rfInfo.message = "[" + orderNo + "]单据已提交，请重新选择单据";
            }
            else
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "未查询到数据，请检查！";
            }
            return rfInfo;
        }
    }
}
